<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="100px">
      <el-row :span="24">
        <el-col :span="6">
          <el-form-item label="模版名称">
            <el-input v-model="tblMain.param.coaName" size="small" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent()">删除</el-button>
      </div>
    </el-form>
    <div>
      <el-table ref="tblMain" style="margin-top: 10px;" :data="tblMain.items" @selection-change="handleSelectionChange"
        v-loading.body="tblMain.loading" border stripe>
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="coaName" width="250" label="费用项名称" align="center"></el-table-column>
        <el-table-column prop="coaPrice" label="费用单价" align="center"></el-table-column>
        <el-table-column prop="coaNumber" label="数量" align="center"></el-table-column>
        <el-table-column prop="pricingUnit" label="计价单位" align="center"></el-table-column>
        <el-table-column prop="financialCycle" label="财务周期" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.financialCycle == '1'">月</span>
            <span v-else-if="scope.row.financialCycle == '2'">季</span>
            <span v-else-if="scope.row.financialCycle == '3'">年</span>
          </template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="合计金额" align="center"></el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize"
        :total="tblMain.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
    </div>
    <expend-dialog ref="modalForm" @ok="modalFormOk"></expend-dialog>
  </div>
</template>
<script>
import ExpendDialog from './components/ExpendDialog'
import tool from '@/tool'
import {
  storeFinanceRecordList,
  storeFinanceRecordDel
} from '@/api/business'
export default ({
  name: 'ExpendList',
  components: {
    ExpendDialog
  },
  data () {
    return {
      tblMain: tool.genTableData({
        param: {
          coaName: '',
        },
        items: []
      }),
    }
  },
  mounted () {
    this.initTableData();
  },
  methods: {
    handleAdd () {
      this.$refs.modalForm.add();
    },
    handleEdit (val) {
      this.$refs.modalForm.edit(val);
    },
    modalFormOk () {
      this.initTableData()
    },
    async deleteEvent () {
      if (this.tblMain.selection.length == 0) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('确定删除选中数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        this.tblMain.selection.forEach((element, index) => {
          params.push(element.id)
        })
        await this.deleteTblMain(params)
      }).catch(() => {
      })
    },
    // 单个删除
    async deleteRow (val) {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        params.push(val.id)
        await this.deleteTblMain(params)
      }).catch(() => {
      })
    },
    // 删除数据
    deleteTblMain (params) {
      let me = this
      me.tblMain.loading = true
      storeFinanceRecordDel({ ids: params }).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success('删除成功')
          this.initTableData()
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    dataReset () {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.coaName = ''
      this.initTableData()
    },
    handleSelectionChange (val) {
      this.tblMain.selection = val;
    },
    handleSizeChange (val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange (val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    initTableData () {
      let me = this
      me.tblMain.selection = []
      let param = {
        coaName: me.tblMain.param.coaName,
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize
      }
      me.tblMain.loading = true
      storeFinanceRecordList(param).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.tblMain.items = data.data.records
          me.tblMain.total = data.data.total
        }
      })
    },
  }
})
</script>
